package dao

import (
	"fmt"
	"log"
	"my-crawler/concurrence/model"
	"my-crawler/concurrence/task"
	"my-crawler/database/mysql"
	"net/url"
)

var uin = task.UIN //我的微信ID
//一个微信生成的key 定时改变 开始爬取前需要手动改变！
//获取方式是用微信浏览器访问一篇指定公众号的文章，然后抓包获取mp/getappmsgext这个路由的参数
var key = task.KEY

func GetComment(id, cid string) (map[string]string, map[string]string, string) {
	var DB = mysql.Db
	var result []model.Articles
	err := DB.Select(&result,
		"select id,link from crawler_articles where id = "+id)

	if err != nil {
		log.Fatal("exec failed, ", err)
	}
	fmt.Println(result)
	m := make(map[string]string)
	parse, _ := url.Parse(result[0].Link)
	urlParam := parse.Query()
	for k, v := range urlParam {
		m[k] = v[0]
	}
	m["id"] = result[0].Id
	//拼url
	var reqUrl = "https://mp.weixin.qq.com/mp/appmsg_comment?__biz=" + m["__biz"] + "&key=" +
		key + "&uin=" + uin + "&idx=" + m["idx"] + "&comment_id=" + cid + "&action=getcomment"
	fmt.Println("reqUrl")
	fmt.Println(reqUrl)
	//Cookie 也是需要变
	header := map[string]string{
		"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36 NetType/WIFI MicroMessenger/7.0.20.1781(0x6700143B) WindowsWechat(0x6304051b)",
		"Cookie":     task.Cookie2,
	}
	return m, header, reqUrl
}
